package view.full.event;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

import rfid.RFIDCardReader;
import event.model.Dsek;
import event.model.Event;
import event.model.User;

public class EventInfoBox implements ActionListener {

	private JFrame frame;
	private JLabel label;
	private Dsek model;
	private Event e;
	private Timer t;

	public EventInfoBox(Dsek model, Event e) {
		frame = new JFrame("Blipp it!");
		this.model = model;
		this.e = e;
		t = new Timer(3000,this);
	}

	public void showAndBlipp() {
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		label = new JLabel();
		frame.add(label);
		label.setText("Blippa!");
		frame.setAlwaysOnTop(true);
		frame.setVisible(true);
		new waiter().start();
	}

	private void reportNoUser() {
		label.setText("Ditt kort finns ej registrerat i databasen.");
	}

	private void showConfirm(User u) {
		label.setText(u.getFirstNane() + " du är nu registrerad.");
	}
	
	private void showAlreadyReg(User u) {
		label.setText(u.getFirstNane() + " du är redan registrerad.");
	}
	
	private void showNotAccepted(User u) {
		label.setText(u.getFirstNane() + " du är redan registrerad men väntar på att bli godkänd.");
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		frame.dispose();
	}
	
	
	private class waiter extends Thread {

		public void run() {
			String card = RFIDCardReader.createRFID().waitAndGetCard();
			
			if (card.equals("5"))
				actionPerformed(null);
			
			int id = model.getIdFromCardId(card);
			
			if (id == -1) {
				reportNoUser();
			} else {
				model.uppdateUsed(id);
				User u = new User(card);
				int status = model.attendingStatus(u, e);
				switch (status) {
				case Dsek.NOT_ATTENDING:
					model.addUserToEvent(u, e);
					showConfirm(u);
					break;
				case Dsek.NOT_ACCEPTED:
					showNotAccepted(u);
					break;
				case Dsek.ATTENDING:
					showAlreadyReg(u);
					break;
				}
			}
			t.start();
			
		}
	}


}
